﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xwt;
using Xwt.Drawing;
using ServiceStack.Logging;
using ServiceStack.Logging.Log4Net;
using g42.Dto;
using g42.ServiceConsumer;
using g42.ServiceProvider;

namespace g42.dev_xwt
{
  public class Startmask: Xwt.Window
  {
    private ServProv sp;
    private ServCons sc;

    private void Init()
    {
      LogManager.LogFactory = new Log4NetFactory(true);
      ILog log;
      log = LogManager.GetLogger("g42.dev-xwt");
      log.Info("start");
      // das läuft im Server
      sp = new ServProv();
      sp.Start(false); // defaultmäßig starten, ohne DB neu zu erstellen
      log.Info("serviceprovider is alive");
      // Das läuft im client
      sc = new ServCons();
      sc.Start();
      log.Info("serviceconsumer is alive");
    }

    public Startmask()
    {
      Init();
      Height = 600;
      Width = 800;
      Title = BuildFormTitle();

      Button bu_Create = new Button("Create Database");
      bu_Create.Clicked += delegate
      {
        sp.Stop();
        sp.Start(true); // true: DatabaseSchema neu erstellen
      };

      Button bu_AddTestData = new Button("Add test data");
      bu_AddTestData.Clicked += delegate
      {
        sc.CreateTestData();
      };

      Label la_SampleRespond = new Label();
      Button bu_Sample = new Button("Call sample service");
      bu_Sample.Clicked += delegate
      {
        // http://enehana.nohea.com/general/customizing-iauthprovider-for-servicestack-net-step-by-step/        
        Sample s = sc.ServiceClient.Post(new Sample { Nr = "1", Bezeichnung = "auth test" });
        la_SampleRespond.Text = "ID = " + s.Id.ToString();
      };

      Table t = new Table();
      t.Add(bu_Create, 0, 10); // Control, Left, Top
      t.Add(bu_AddTestData, 0, 40);
      t.Add(bu_Sample, 0, 70);
      t.Add(la_SampleRespond, 50, 70); 

      Content = t;

    }

    private String BuildFormTitle()
    {
      String AppName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
      // String FormTitle = String.Format("{0} {1} ({2})", AppName, Application.ProductName, Application.ProductVersion);
      String FormTitle = AppName;
      return FormTitle;
    }
  }
}

